#region Using

using System;
using System.Net;
using WSMon.Core.Configuration;
using WSMon.Core.Helpers;
using WSMon.Core.Interfaces;

#endregion

namespace WSMon.Core.Checkers.Web
{
    public class HttpChecker : Checker, IChecker
    {
        private readonly HttpRequestConfiguration _httpRequestConfiguration;
        private string _logs = string.Empty;

        public HttpChecker(HttpRequestConfiguration httpRequestConfiguration)
        {
            if (httpRequestConfiguration == null)
                throw new ArgumentNullException("httpRequestConfiguration");

            _httpRequestConfiguration = httpRequestConfiguration;
        }

        #region IChecker Members

        public bool CheckOut()
        {
            try
            {
                HttpWebResponse webResponse = HttpRequestHelper.GetWebResponse(_httpRequestConfiguration.Url,
                                                                               new NetworkCredential(
                                                                                   _httpRequestConfiguration.UserName,
                                                                                   _httpRequestConfiguration.Password));
                return webResponse.StatusCode == HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                _logs = ex.Message;
                return false;
            }
        }

        public string GetLogs()
        {
            return _logs;
        }

        #endregion
    }
}